package com.tfswx.jcyw.dmzc.codeInspection;

import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.JavaElementVisitor;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiWhiteSpace;
import org.jetbrains.annotations.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ZwblInspection extends AbstractBaseJavaLocalInspectionTool {
    private static final Pattern ZW_ZF = Pattern.compile("[\\u4E00-\\u9FA5！，。（）《》“”？：；【】]");

    public static boolean hasZw(CharSequence input) {
        Matcher m = ZwblInspection.ZW_ZF.matcher(input);
        return m.find();
    }

    @Override
    public @NotNull PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {
        return new JavaElementVisitor() {
            @Override
            public void visitField(PsiField field) {
                super.visitField(field);
                if (ZwblInspection.hasZw(field.getName())) {
                    holder.registerProblem(field, "字段命名不能包含中文", ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
                }

            }

            @Override
            public void visitWhiteSpace(@NotNull PsiWhiteSpace space) {
                super.visitWhiteSpace(space);
            }
        };
    }
}
